local sk__fanghun = fk.CreateSkill {

  name = "sk__fanghun",

  tags = {},

}



sk__fanghun:addEffect("viewas", {
  name = "sk__fanghun",
  pattern = "slash,jink",
  prompt = "#sk__fanghun",
  card_filter = function(self, player, to_select, selected)
    if #selected == 0 then
      local c = Fk:getCardById(to_select)
      local card
      if c.trueName == "slash" then
        card = Fk:cloneCard("jink")
      elseif c.trueName == "jink" then
        card = Fk:cloneCard("slash")
      else
        return false
      end
      return (Fk.currentResponsePattern == nil and player:canUse(card)) or
          (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card))
    end
  end,
  view_as = function(self, player, cards)
    if #cards ~= 1 then return end
    local c = Fk:getCardById(cards[1])
    local card
    if c.trueName == "slash" then
      card = Fk:cloneCard("jink")
    elseif c.name == "jink" then
      card = Fk:cloneCard("slash")
    end
    card.skillName = sk__fanghun.name
    card:addSubcards(cards)
    return card
  end,
})

sk__fanghun:addEffect(fk.CardUsing, {
  name = "#sk__fanghun_trigger",
  --sk__fanghun,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(sk__fanghun.name) and table.contains(data.card.skillNames, "sk__fanghun") then
      local id
      if data.card.trueName == "slash" then
        id = data.tos[1].id
      elseif data.card.name == "jink" then
        if data.responseToEvent then
          id = data.responseToEvent.from.id --jink
        end
      end
      if id ~= nil then
        event:setCostData(self, id)
        return not player.room:getPlayerById(id):isKongcheng()
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, "sk__fanghun", nil, "#sk__fanghun-invoke::" .. event:getCostData(self))
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(event:getCostData(self))
    player:broadcastSkillInvoke("sk__fanghun")
    room:notifySkillInvoked(player, "sk__fanghun", "offensive")
    room:doIndicate(player.id, { to.id })
    local card = room:askForCardChosen(player, to, "h", "sk__fanghun")
    room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonPrey, "sk__fanghun", nil, false, player)
  end,
})
sk__fanghun:addEffect(fk.CardResponding, {
  name = "#sk__fanghun_trigger",
  --sk__fanghun,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(sk__fanghun.name) and table.contains(data.card.skillNames, "sk__fanghun") then
      local id
      if data.responseToEvent then
        if data.responseToEvent.from == player then
          id = data.responseToEvent.to.id   --duel used by zhaoyun
        else
          id = data.responseToEvent.from.id --savsavage_assault, archery_attack, passive duel
          --TODO: Lenovo shu zhaoyun may chongzhen liubei when responding to jijiang
        end
      end
      if id ~= nil then
        event:setCostData(self, id)
        return not player.room:getPlayerById(id):isKongcheng()
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, "sk__fanghun", nil, "#sk__fanghun-invoke::" .. event:getCostData(self))
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(event:getCostData(self))
    player:broadcastSkillInvoke("sk__fanghun")
    room:notifySkillInvoked(player, "sk__fanghun", "offensive")
    room:doIndicate(player.id, { to.id })
    local card = room:askForCardChosen(player, to, "h", "sk__fanghun")
    room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonPrey, "sk__fanghun", nil, false, player)
  end,
})

return sk__fanghun
